package main.java.peopleinfo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;

public class PeopleInfoMain {

    public static void main(String[] args) throws Exception {


//		copyFile(new Configuration(),"hdfs://10.16.7.36:9000","/Users/zhangyimin/training/data/input/sample_people_info.txt","/data/input/sogou");

        PeopleInfo peopleInfo = new PeopleInfo();
        peopleInfo.getGender();

        //1??????Job
        Job job = Job.getInstance(new Configuration());
        job.setJarByClass(PeopleInfoMain.class);

        //2??????????Mapper???????????
        job.setMapperClass(PeopleInfoMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(PeopleInfo.class);

        //3??????????Reducer???????????
        job.setReducerClass(PeopleInfoReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //4???????????????
//		FileInputFormat.setInputPaths(job, new Path(args[0]));
//		FileOutputFormat.setOutputPath(job, new Path(args[1]));

        FileInputFormat.setInputPaths(job, new Path("hdfs://10.16.7.36:9000/data/input/sogou/sample_people_info.txt"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://10.16.7.36:9000/data/output/hdfs"+Math.random()*1000));


        //5?????
        job.waitForCompletion(true);
    }


    public static void copyFile(Configuration conf, String uri, String local, String remote) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        fs.copyFromLocalFile(new Path(local), new Path(remote));
        System.out.println("copy from: " + local + " to " + remote);
        fs.close();
    }


}
